package comp.wxapi.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import comp.wxapi.bean.Result;
import comp.wxapi.utils.StorageServiceUtils;
import comp.wxapi.vo.MaterialVo;
import weixin.popular.api.MediaAPI;
import weixin.popular.bean.media.Media;
import weixin.popular.bean.media.MediaGetResult;
import weixin.popular.bean.media.MediaType;

@Service
public class MaterialService extends WxAbstractBaseService {
	
	public String getTempMaterial(String mediaId,String accessToken) {
		MediaGetResult result = MediaAPI.mediaGet(accessToken, mediaId);
		if(StringUtils.isNotBlank(result.getVideo_url())) {
		
			return JSON.toJSONString(Result.ok(result));
		}
		return JSON.toJSONString(Result.ok());
	}
	
	public String addTempMaterial(MaterialVo materialVo,String accessToken) throws Exception {
		String downloadURL = StorageServiceUtils.getDownloadURL(host(), StorageServiceUtils.getStoreFileName(materialVo.getStoreFileName()));
		MediaType mediaType = null;
		switch (materialVo.getMaterialType()) {
			case "image":
				mediaType = MediaType.image;
				break;
			case "voice-mp3":
				mediaType = MediaType.voice_mp3;
				break;
			case "voice-arm":
				mediaType = MediaType.voice_arm;
				break;
			case "video":
				mediaType = MediaType.video;
				break;
			case "thumb":
				mediaType = MediaType.thumb;
				break;
			default:
				break;
		}
		Media material = MediaAPI.mediaUpload(accessToken, mediaType, StorageServiceUtils.getFileInputStream(downloadURL));
		return JSON.toJSONString(Result.ok(material));
	}
}
